<template>
  <el-select :value="valueTitle" :clearable="clearable" @clear="clearHandle" size="small">
    <el-option :value="valueTitle" :label="valueTitle" class="options">
      <el-tree
        ref="selectTree"
        :accordion="accordion"
        :data="options"
        :load="loadNode"
        check-on-click-node
        :check-strictly="1==1"
        :show-checkbox="config.multiple"
        :props="config"
        lazy
        :default-expanded-keys="defaultExpandedKey"
        @check-change="handleCheckChange"
        @node-click="handleNodeClick">
      </el-tree>
    </el-option>
  </el-select>
</template>

<script>
  import request from "@/utils/requestutil";
  export default {
    name: "el-tree-select",
    props:{
      value:{type:String,default:()=>""},
      // 配置项
      option:{
        type: Object,
        default:()=>{return {
          value:'id',
          label: 'name',
          children: 'children'
        }
        }
      },
      clearable:{ type:Boolean, default: true },
      accordion:{ type:Boolean, default: false }
    },
    data() {
      return {
        defaultDataUrl:'normalDicTree',
        valueId: null,
        dicKey:'',
        dataRoot:'data',
        valueTitle:'',
        defaultExpandedKey:[],
        options:[],
        config:{
           value:'id',
          label: 'name',
          children: 'children',
          isLeaf:"isLeaf"
        }
      }
    },
    mounted(){
    },
    methods: {
      isLeaf(data,node){
        if(this.option.isLeaf){
          return this.option.isLeaf(data,node);
        }
        let r=this.children&&this.children.length > 0 
        return !r;
      },
      loadNode(node, resolve){
        if(this.config.dataUrl==undefined && this.dicKey==undefined){
          resolve([]);
          return;
        }
        let nodedata=node.data;
        let url=this.config.dataUrl||this.defaultDataUrl;
        let param={p0:nodedata.id};
        if(this.config.beforeLoad){
          param=this.config.beforeLoad(param,nodedata)
        }
        let dataRoot=this.config.dataRoot||this.dataRoot;
        request({
          url:url,
          urlparam:[this.config.dicKey],
          data:param,
          method:'post'
        }).then((res)=>{
          let data=res[dataRoot];
          if(data==undefined)resolve([]);
          if(this.config.dataFilter){
            data=this.config.dataFilter(data);
          }
          data=this.dataFilter(data);
          resolve(data);
        })
      },
      dataFilter(data){
        return data;
      },
      initHandle(){
        // debugger
        // console.log(this.$refs.selectTree)
        // console.log(this.$refs.selectTree.getNode(this.valueId))
        if (this.$refs.selectTree.getNode(this.valueId)){
          this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.config.label]
          this.$refs.selectTree.setCurrentKey(this.valueId)
          this.defaultExpandedKey = [this.valueId]
          this.initScroll()
        }

      },
      initScroll(){
        this.$nextTick(()=>{
          let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
          let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
          scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
          scrollBar.forEach(ele => ele.style.width = 0)
        })
      },
      handleCheckChange(node,checked){
        if(!this.config.multiple)return;
        let checknodes=this.$refs.selectTree.getCheckedNodes();
        this.valueTitle = [];
        this.valueId = [];
        for(let i=0;i<checknodes.length;i++){
            this.valueTitle.push(node[this.config.label])
            this.valueId.push(node[this.config.value]);
        }
        this.valueTitle=this.valueTitle.join(",");
        this.valueId=this.valueId.join(",")
      },
      handleNodeClick(node){
        if(this.config.multiple)return;
        this.valueTitle = node[this.config.label]
        this.valueId = node[this.config.value]
        this.$emit('change',this.valueId)
        this.defaultExpandedKey = []
      },
      clearHandle(){
        this.valueTitle = ''
        this.valueId = null
        this.defaultExpandedKey = []
        this.clearSelected()
        this.$emit('getValue',null)
      },
      clearSelected(){
        let allNode = document.querySelectorAll('#tree-option .el-tree-node')
        allNode.forEach((element)=>element.classList.remove('is-current'))
      }
    },
    watch: {
      value(){
        this.valueId = this.value
        this.initHandle()
      },
      option:{
        handler(val){
        let cfg=this.$_.cloneDeep(this.config);
        this.config=Object.assign({},cfg,val);
        this.dicKey=val.dicKey;
      },
      immediate:true
      }
    },
  }
</script>

<style scoped>
  .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
    height: auto;
    max-height: 274px;
    padding: 0;
    overflow: hidden;
    overflow-y: auto;
  }
  .el-select-dropdown__item.selected{
    font-weight: normal;
  }
  ul li >>>.el-tree .el-tree-node__content{
    height:auto;
    padding: 0 20px;
  }
  .el-tree-node__label{
    font-weight: normal;
  }
  .el-tree >>>.is-current .el-tree-node__label{
    color: #409EFF;
    font-weight: 700;
  }
  .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
    color:#606266;
    font-weight: normal;
  }
</style>
